package com.aidong.login.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.drawCode.biz.DrawBiz;
import com.aidong.drawCode.entity.DrawCode;
import com.aidong.jobs.async.SmsTask;
import com.aidong.jobs.constants.JobConst;
import com.aidong.login.biz.MemberReigsterBiz;
import com.aidong.login.entity.Member;
import com.aidong.util.RandomUtil;
import com.aidong.util.StringUtil;
import com.aidong.util.bus.SecurityUtils;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 登陆注册action
 * 
 * @author java
 * @version 1.0 2015.6.10
 */
@ParentPackage("json")
@Namespace("/")
public class MemberReigsterAction implements ModelDriven<Member> {
	private Member member; // 会员信息

	@Override
	public Member getModel() {
		if (member == null) {
			member = new Member();
		}
		return member;
	}

	@Autowired
	private MemberReigsterBiz memberBiz; // 登陆注册service
	@Autowired
	SmsTask smsTask;
	@Autowired
	private DrawBiz drawBiz;
	private Map<String, Object> map; // 返回的json数据

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	/**
	 * 注册验证码 获得手机验证码
	 * 
	 * @return
	 */
	@Action(value = "getphonecode", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getPhoneCode() {
		map = new HashMap<String, Object>();
		try {
			String tel = member.getMobilePhoneEncrypt();
			String codes = member.getCode();//验证码
			if(StringUtils.isBlank(codes)){
				map.put("code", 0);
				map.put("msg", "请下载最新版本");
				return "succ";
			}
			// 手机号码解密
			member.setMobilePhone(SecurityUtils.decryptPhoneNo(tel));
			String phoneNo = member.getMobilePhone().trim(); // 得到提交的手机号码
			
			String[] codeMap=JobConst.codeMap.get(phoneNo);
			if(codeMap==null){
				map.put("code", 0);
				map.put("msg", "验证码已过期");
				return "succ";
			}
			
			if (phoneNo != null && !"".equals(phoneNo)) {
				if(!codes.equalsIgnoreCase(codeMap[0])){
					map.put("code", 0);
					map.put("msg", "验证码输入有误");
					return "succ";
				}
				JobConst.codeMap.remove(phoneNo);
				int count = memberBiz.getRegistered(phoneNo);
				if (count > 0) { // 说明该手机号码已注册
					map.put("code", 0);
					map.put("msg", "该手机号码已注册");
					return "succ";
				} else {
					String captcha = RandomUtil.randomNumber(6);
					int code=Integer.valueOf(captcha);
					smsTask.captcha(phoneNo,String.valueOf(code)); // 手机验证码
					map.put("code", 1);
					map.put("result", code);
					return "succ";
				}
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 设置密码（注册成功）
	 * 
	 * @return
	 */
	@Action(value = "setpassword", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String setPassword() {
		map = new HashMap<String, Object>();
		try {
			// 手机号码解密
			member.setMobilePhone(SecurityUtils.decryptPhoneNo(member.getMobilePhoneEncrypt()));
			String telNo = member.getMobilePhone(); // 手机号码

			member.setPassword(SecurityUtils.encryptDbMemberPassword(member.getPassword()));
			String password = member.getPassword(); // 密码
			if ((telNo != null && !"".equals(telNo)) && (password != null && !"".equals(password))) {
				// 判断是否已经注册
				int count = memberBiz.getRegistered(telNo);
				if (count > 0) {
					map.put("code", 0);
					map.put("msg", "该手机号码已注册");
					return "succ";
				} else {
					// 添加新用户
					Map<String, Object> regMap = new HashMap<String, Object>();
					regMap.put("mobilePhone", telNo);
					regMap.put("password", password);
					regMap.put("loginType", 1);
					memberBiz.addMember(regMap);
					/**
					 * 新用户注册生成10元优惠券
					 */
					try{
						memberBiz.addTicketsByNewbie(regMap);
					}catch(Exception e){
					}
					int cou=drawBiz.getDrawCountByNowTime(new Date());
					if(cou>0){
						//为新用户生成码
						DrawCode code=new DrawCode();
						code.setIdongId(Integer.parseInt(regMap.get("idongId").toString()));
						code.setCode(RandomUtil.randDrawCode(4));
						code.setBak("注册");
						drawBiz.addDrawCode(code);
					}
					
					map.put("code", 1);
					map.put("idongIdEncrypt",
							SecurityUtils.encryptIdongId(StringUtil.convertInt(regMap.get("idongId"))));
					map.put("msg", "下一步");
					return "succ";
				}
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

}